package com.songlanyun.modules.distributionconfig.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.songlanyun.common.enums.GoodsConstant;
import com.songlanyun.common.exception.RRException;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.Query;
import com.songlanyun.common.validator.ValidatorUtils;
import com.songlanyun.modules.distributionconfig.dao.DistributionShopGoodsConfigDao;
import com.songlanyun.modules.distributionconfig.dao.DistributionShopServiceGoodsConfigDao;
import com.songlanyun.modules.distributionconfig.entity.DistributionShopConfig;
import com.songlanyun.modules.distributionconfig.entity.DistributionShopGoodsConfig;
import com.songlanyun.modules.distributionconfig.entity.DistributionShopServiceGoodsConfig;
import com.songlanyun.modules.distributionconfig.model.DistributionShopGoodsConfigDTO;
import com.songlanyun.modules.distributionconfig.model.DistributionShopServiceGoodsConfigDTO;
import com.songlanyun.modules.distributionconfig.service.DistributionShopConfigService;
import com.songlanyun.modules.distributionconfig.service.DistributionShopGoodsConfigService;
import com.songlanyun.modules.distributionconfig.service.DistributionShopServiceGoodsConfigService;
import com.songlanyun.modules.goods.entity.Goods;
import com.songlanyun.modules.goods.service.GoodsService;
import com.songlanyun.modules.servicegoods.entity.ServiceGoods;
import com.songlanyun.modules.servicegoods.service.ServiceGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;


@Service("distributionShopServiceGoodsConfigService")
public class DistributionShopServiceGoodsConfigServiceImpl extends ServiceImpl<DistributionShopServiceGoodsConfigDao, DistributionShopServiceGoodsConfig> implements DistributionShopServiceGoodsConfigService {

    @Autowired
    private DistributionShopConfigService distributionShopConfigService;
    @Autowired
    private ServiceGoodsService serviceGoodsService;

    @Override
    public DistributionShopServiceGoodsConfig create(DistributionShopServiceGoodsConfig entity) {
        ValidatorUtils.validateEntity(entity);
        this.baseMapper.insert(entity);
        return entity;
    }

    @Override
    public DistributionShopServiceGoodsConfig modify(DistributionShopServiceGoodsConfig entity) {
        ValidatorUtils.validateEntity(entity);
        this.baseMapper.updateById(entity);
        return entity;
    }

    @Override
    public void remove(Long id) {

    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<DistributionShopServiceGoodsConfig> page = this.page(
                new Query<DistributionShopServiceGoodsConfig>().getPage(params),
                new QueryWrapper<DistributionShopServiceGoodsConfig>()
        );

        return new PageUtils(page);
    }

    /**
     * 获取商家服务商品级分销设置
     * @param shopId
     * @param goodsId
     * @return
     */
    @Override
    public DistributionShopServiceGoodsConfig getShopAndGoods(Long shopId, Long goodsId) {
        return  this.getOne(new LambdaQueryWrapper<DistributionShopServiceGoodsConfig>()
                .eq(DistributionShopServiceGoodsConfig::getShopId, shopId)
                .eq(DistributionShopServiceGoodsConfig::getGoodsId, goodsId)
                .last("limit 1"));
    }

    /**
     * 保存服务商品级分销设置
     * @param shopId
     * @param dto
     * @return
     */
    @Transactional
    @Override
    public DistributionShopServiceGoodsConfig set(Long shopId, DistributionShopServiceGoodsConfigDTO dto) {

        DistributionShopConfig shopConfig = distributionShopConfigService.getByShop(shopId);
        if(shopConfig==null)
        {
            throw new RRException("店铺级的一二级分佣还没有设置");
        }

        ServiceGoods goods = serviceGoodsService.loadServiceGoodsByShopIdAndId(shopId, dto.getGoodsId(),false);
        DistributionShopServiceGoodsConfig config = this.getShopAndGoods(shopId, dto.getGoodsId());
        if(config==null)
        {
            DistributionShopServiceGoodsConfig newConfig = new DistributionShopServiceGoodsConfig();
            newConfig.setShopId(shopId);
            newConfig.setGoodsId(goods.getId());
            newConfig.setComputeMode(GoodsConstant.ComputeMode.getByCode(dto.getComputeMode()));
            newConfig.setDefaultShop(dto.getDefaultShop());
            newConfig.setFirstLevelPrice(dto.getFirstLevelPrice());
            newConfig.setSecondLevelPrice(dto.getSecondLevelPrice());
            newConfig.setFirstLevelRatio(dto.getFirstLevelRatio());
            newConfig.setSecondLevelRatio(dto.getSecondLevelRatio());
            newConfig.setOpen(dto.getOpen());
            this.create(newConfig);
            return newConfig;
        }
        else
        {
            config.setComputeMode(GoodsConstant.ComputeMode.getByCode(dto.getComputeMode()));
            config.setDefaultShop(dto.getDefaultShop());
            config.setFirstLevelPrice(dto.getFirstLevelPrice());
            config.setSecondLevelPrice(dto.getSecondLevelPrice());
            config.setFirstLevelRatio(dto.getFirstLevelRatio());
            config.setSecondLevelRatio(dto.getSecondLevelRatio());
            config.setOpen(dto.getOpen());
            this.modify(config);
            return config;

        }
    }
}